jQuery(function() {
    var uploader = WebUploader.create({
        swf: BASE_URL + '/Uploader.swf',
        server: server,
        pick: {
            id: '#filePicker',
            label: '点击选择图片'
        },
        resize: false,
        threads:5,
        auto:false,
        formData:{'_token':token,'folder':folder},
    });

    $upload.on('click',function(){
        uploader.upload();
    });

    uploader.on('fileQueued',function( file ) {
        //if($("div[id^='WU_FILE']")){
        //    $("div[id^='WU_FILE']").remove();
        //}
        var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                    '<img>' +
                    '<div class="info" style="text-align:center;">' + file.name + '</div>' +
                '</div>'
                ),
            $img = $li.find('img');

        $wrap.append( $li );

        // 创建缩略图
        // 如果为非图片文件，可以不用调用此方法。
        uploader.makeThumb( file, function( error, src ) {
            if ( error ) {
                $img.replaceWith('<span>不能预览</span>');
                return;
            }

            $img.attr( 'src', src );
        }, thumbnailWidth, thumbnailHeight );
    });

    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
              '<div class="progress-bar" role="progressbar" style="width: 0%">' +
              '</div>' +
            '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('.info').text('上传中');

        $percent.css( 'width', percentage * 100 + '%' );
    });

    uploader.on( 'uploadSuccess', function( file,response ) {
        if(response.response_code==1){
            if(typeof(handle) != "undefined"){//如果有选择器则加上选择器选中
                $('input#'+handle+'[name='+img_name+']').val(response.data);
            }else{
                $('input[name='+img_name+']').val(response.data);
            }
            $( '#'+file.id ).find('.info').text('已上传');
        }
    });

    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('.info').text('上传出错');
    });

    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });
});